﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for ProductsController
/// </summary>
public class ProductsController : Interface
{
    private SqlConnection con;
    public ProductsController()
    {
        con = ConnectDB.Connect();
        ConnectDB.Open();
    }

    public int Insert(object obj)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Insert_Product";
            cmd.CommandType = CommandType.StoredProcedure;
            Products p = (Products)obj;

            cmd.Parameters.Add("@model", SqlDbType.NText).Value = p.Model;
            cmd.Parameters.Add("@url", SqlDbType.NText).Value = p.Url;
            cmd.Parameters.Add("@category_id", SqlDbType.Int).Value = p.Category_id;
            cmd.Parameters.Add("@dateStart", SqlDbType.DateTime).Value = p.DateStart;
            cmd.Parameters.Add("@tag", SqlDbType.NText).Value = p.Tag;
            cmd.Parameters.Add("@status", SqlDbType.Bit).Value = p.Status;
            cmd.Parameters.Add("@price_input", SqlDbType.Float).Value = p.Price_input;
            cmd.Parameters.Add("@price_output", SqlDbType.Float).Value = p.Price_output;
            cmd.Parameters.Add("@baohanh_id", SqlDbType.Int).Value = p.Baohanh_id;
            cmd.Parameters.Add("@view", SqlDbType.Int).Value = p.View;
            cmd.Parameters.Add("@top", SqlDbType.Bit).Value = p.Top;
            cmd.Parameters.Add("@address_baohanh_id", SqlDbType.Int).Value = p.AddressBaohanh_id;
            cmd.Parameters.Add("@quantity", SqlDbType.Int).Value = p.Quantity;
            cmd.Parameters.Add("@tinhnangnoibat", SqlDbType.NText).Value = p.Tinhnangnoibat;
            cmd.Parameters.Add("@trangthaiphathanh", SqlDbType.NText).Value = p.Trangthaiphathanh;
            cmd.Parameters.Add("@ngaycongbo", SqlDbType.NText).Value = p.Ngaycongbo;
            cmd.Parameters.Add("@ngayphathanh", SqlDbType.NText).Value = p.Ngayphathanh;
            cmd.Parameters.Add("@bangtan", SqlDbType.NText).Value = p.Bangtan;
            cmd.Parameters.Add("@chieudai", SqlDbType.NText).Value = p.Chieudai;
            cmd.Parameters.Add("@chieurong", SqlDbType.NText).Value = p.Chieurong;
            cmd.Parameters.Add("@chieucao", SqlDbType.NText).Value = p.Chieucao;
            cmd.Parameters.Add("@cannang", SqlDbType.NText).Value = p.Cannang;
            cmd.Parameters.Add("@kieumanhinh", SqlDbType.NText).Value = p.Kieumanhinh;
            cmd.Parameters.Add("@kichthuocmanhinh", SqlDbType.NText).Value = p.Kichthuocmanhinh;
            cmd.Parameters.Add("@dophangiai", SqlDbType.NText).Value = p.Dophangiai;
            cmd.Parameters.Add("@camung", SqlDbType.NText).Value = p.Camung;
            cmd.Parameters.Add("@tinhnangkhac", SqlDbType.NText).Value = p.Tinhnangkhac;
            cmd.Parameters.Add("@tinnhan", SqlDbType.NText).Value = p.Tinnhan;
            cmd.Parameters.Add("@jackcam", SqlDbType.NText).Value = p.Jackcam;
            cmd.Parameters.Add("@tainhacchuong", SqlDbType.NText).Value = p.Tainhacchuong;
            cmd.Parameters.Add("@loangoai", SqlDbType.NText).Value = p.Loangoai;
            cmd.Parameters.Add("@danhbaluutru", SqlDbType.NText).Value = p.Danhbaluutru;
            cmd.Parameters.Add("@hotrosms", SqlDbType.NText).Value = p.Hotrosms;
            cmd.Parameters.Add("@hotroemail", SqlDbType.NText).Value = p.Hotroemail;
            cmd.Parameters.Add("@bonhotrong", SqlDbType.NText).Value = p.Bonhotrong;
            cmd.Parameters.Add("@dungluongthenho", SqlDbType.NText).Value = p.Dungluongthenho;
            cmd.Parameters.Add("@loaithenho", SqlDbType.NText).Value = p.Loaithenho;
            cmd.Parameters.Add("@gprs", SqlDbType.NText).Value = p.Gprs;
            cmd.Parameters.Add("@edge", SqlDbType.NText).Value = p.Edge;
            cmd.Parameters.Add("@3g", SqlDbType.NText).Value = p.BaG;
            cmd.Parameters.Add("@web", SqlDbType.NText).Value = p.Web;
            cmd.Parameters.Add("@gps", SqlDbType.NText).Value = p.Gps;
            cmd.Parameters.Add("@bluetooth", SqlDbType.NText).Value = p.Bluetooth;
            cmd.Parameters.Add("@wifi", SqlDbType.NText).Value = p.Wifi;
            cmd.Parameters.Add("@hongngoai", SqlDbType.NText).Value = p.Hongngoai;
            cmd.Parameters.Add("@usb", SqlDbType.NText).Value = p.Usb;
            cmd.Parameters.Add("@mayanhchinh", SqlDbType.NText).Value = p.Mayanhchinh;
            cmd.Parameters.Add("@mayanhkhac", SqlDbType.NText).Value = p.Mayanhkhac;
            cmd.Parameters.Add("@mayanhphu", SqlDbType.NText).Value = p.Mayanhphu;
            cmd.Parameters.Add("@quayphim", SqlDbType.NText).Value = p.Quayphim;
            cmd.Parameters.Add("@nghenhac", SqlDbType.NText).Value = p.Nghenhac;
            cmd.Parameters.Add("@xemphim", SqlDbType.NText).Value = p.Xemphim;
            cmd.Parameters.Add("@radio", SqlDbType.NText).Value = p.Radio;
            cmd.Parameters.Add("@tivi", SqlDbType.NText).Value = p.Tivi;
            cmd.Parameters.Add("@ketnoitivi", SqlDbType.NText).Value = p.Ketnoitivi;
            cmd.Parameters.Add("@ghiam", SqlDbType.NText).Value = p.Ghiam;
            cmd.Parameters.Add("@luutrucuocgoi", SqlDbType.NText).Value = p.Luutrucuocgoi;
            cmd.Parameters.Add("@ungdungvanphong", SqlDbType.NText).Value = p.Ungdungvanphong;
            cmd.Parameters.Add("@ungdungkhac", SqlDbType.NText).Value = p.Ungdungkhac;
            cmd.Parameters.Add("@trochoi", SqlDbType.NText).Value = p.Trochoi;
            cmd.Parameters.Add("@hotrosim", SqlDbType.NText).Value = p.Hotrosim;
            cmd.Parameters.Add("@hotrojava", SqlDbType.NText).Value = p.Hotrojava;
            cmd.Parameters.Add("@kieudang", SqlDbType.NText).Value = p.Kieudang;
            cmd.Parameters.Add("@hedieuhanh", SqlDbType.NText).Value = p.Hedieuhanh;
            cmd.Parameters.Add("@phienbanhedieuhanh", SqlDbType.NText).Value = p.Phienbanhedieuhanh;
            cmd.Parameters.Add("@cpu", SqlDbType.NText).Value = p.Cpu;
            cmd.Parameters.Add("@ram", SqlDbType.NText).Value = p.Ram;
            cmd.Parameters.Add("@ngonngu", SqlDbType.NText).Value = p.Ngonngu;
            cmd.Parameters.Add("@qwerty", SqlDbType.NText).Value = p.Qwerty;
            cmd.Parameters.Add("@hotromang", SqlDbType.NText).Value = p.Hotromang;
            cmd.Parameters.Add("@loaipin", SqlDbType.NText).Value = p.Loaipin;
            cmd.Parameters.Add("@thoigiancho2g", SqlDbType.NText).Value = p.Thoigiancho2g;
            cmd.Parameters.Add("@thoigiancho3g", SqlDbType.NText).Value = p.Thoigiancho3g;
            cmd.Parameters.Add("@damthoai2g", SqlDbType.NText).Value = p.Damthoai2g;
            cmd.Parameters.Add("@damthoai3g", SqlDbType.NText).Value = p.Damthoai3g;
            cmd.Parameters.Add("@dungluongpin", SqlDbType.NText).Value = p.Dungluongpin;
            cmd.Parameters.Add("@image", SqlDbType.NText).Value = p.Image;
            cmd.Parameters.Add("@description", SqlDbType.NText).Value = p.Description;
            object result = cmd.ExecuteScalar();
            return Convert.ToInt32(result);
        }
        catch (Exception ex)
        {
            return 0;
        }
    }

    public int Update(object obj)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Update_Product";
            cmd.CommandType = CommandType.StoredProcedure;
            Products p = (Products)obj;
            cmd.Parameters.Add("@product_id", SqlDbType.Int).Value = p.Product_id;
            cmd.Parameters.Add("@model", SqlDbType.NText).Value = p.Model;
            cmd.Parameters.Add("@url", SqlDbType.NText).Value = p.Url;
            cmd.Parameters.Add("@category_id", SqlDbType.Int).Value = p.Category_id;
            cmd.Parameters.Add("@dateUpdate", SqlDbType.DateTime).Value = p.DateUpdate;
            cmd.Parameters.Add("@tag", SqlDbType.NText).Value = p.Tag;
            cmd.Parameters.Add("@status", SqlDbType.Bit).Value = p.Status;
            cmd.Parameters.Add("@price_input", SqlDbType.Float).Value = p.Price_input;
            cmd.Parameters.Add("@price_output", SqlDbType.Float).Value = p.Price_output;
            cmd.Parameters.Add("@baohanh_id", SqlDbType.Int).Value = p.Baohanh_id;
            cmd.Parameters.Add("@view", SqlDbType.Int).Value = p.View;
            cmd.Parameters.Add("@top", SqlDbType.Bit).Value = p.Top;
            cmd.Parameters.Add("@address_baohanh_id", SqlDbType.Int).Value = p.AddressBaohanh_id;
            cmd.Parameters.Add("@quantity", SqlDbType.Int).Value = p.Quantity;
            cmd.Parameters.Add("@tinhnangnoibat", SqlDbType.NText).Value = p.Tinhnangnoibat;
            cmd.Parameters.Add("@trangthaiphathanh", SqlDbType.NText).Value = p.Trangthaiphathanh;
            cmd.Parameters.Add("@ngaycongbo", SqlDbType.NText).Value = p.Ngaycongbo;
            cmd.Parameters.Add("@ngayphathanh", SqlDbType.NText).Value = p.Ngayphathanh;
            cmd.Parameters.Add("@bangtan", SqlDbType.NText).Value = p.Bangtan;
            cmd.Parameters.Add("@chieudai", SqlDbType.NText).Value = p.Chieudai;
            cmd.Parameters.Add("@chieurong", SqlDbType.NText).Value = p.Chieurong;
            cmd.Parameters.Add("@chieucao", SqlDbType.NText).Value = p.Chieucao;
            cmd.Parameters.Add("@cannang", SqlDbType.NText).Value = p.Cannang;
            cmd.Parameters.Add("@kieumanhinh", SqlDbType.NText).Value = p.Kieumanhinh;
            cmd.Parameters.Add("@kichthuocmanhinh", SqlDbType.NText).Value = p.Kichthuocmanhinh;
            cmd.Parameters.Add("@dophangiai", SqlDbType.NText).Value = p.Dophangiai;
            cmd.Parameters.Add("@camung", SqlDbType.NText).Value = p.Camung;
            cmd.Parameters.Add("@tinhnangkhac", SqlDbType.NText).Value = p.Tinhnangkhac;
            cmd.Parameters.Add("@tinnhan", SqlDbType.NText).Value = p.Tinnhan;
            cmd.Parameters.Add("@jackcam", SqlDbType.NText).Value = p.Jackcam;
            cmd.Parameters.Add("@tainhacchuong", SqlDbType.NText).Value = p.Tainhacchuong;
            cmd.Parameters.Add("@loangoai", SqlDbType.NText).Value = p.Loangoai;
            cmd.Parameters.Add("@danhbaluutru", SqlDbType.NText).Value = p.Danhbaluutru;
            cmd.Parameters.Add("@hotrosms", SqlDbType.NText).Value = p.Hotrosms;
            cmd.Parameters.Add("@hotroemail", SqlDbType.NText).Value = p.Hotroemail;
            cmd.Parameters.Add("@bonhotrong", SqlDbType.NText).Value = p.Bonhotrong;
            cmd.Parameters.Add("@dungluongthenho", SqlDbType.NText).Value = p.Dungluongthenho;
            cmd.Parameters.Add("@loaithenho", SqlDbType.NText).Value = p.Loaithenho;
            cmd.Parameters.Add("@gprs", SqlDbType.NText).Value = p.Gprs;
            cmd.Parameters.Add("@edge", SqlDbType.NText).Value = p.Edge;
            cmd.Parameters.Add("@3g", SqlDbType.NText).Value = p.BaG;
            cmd.Parameters.Add("@web", SqlDbType.NText).Value = p.Web;
            cmd.Parameters.Add("@gps", SqlDbType.NText).Value = p.Gps;
            cmd.Parameters.Add("@bluetooth", SqlDbType.NText).Value = p.Bluetooth;
            cmd.Parameters.Add("@wifi", SqlDbType.NText).Value = p.Wifi;
            cmd.Parameters.Add("@hongngoai", SqlDbType.NText).Value = p.Hongngoai;
            cmd.Parameters.Add("@usb", SqlDbType.NText).Value = p.Usb;
            cmd.Parameters.Add("@mayanhchinh", SqlDbType.NText).Value = p.Mayanhchinh;
            cmd.Parameters.Add("@mayanhkhac", SqlDbType.NText).Value = p.Mayanhkhac;
            cmd.Parameters.Add("@mayanhphu", SqlDbType.NText).Value = p.Mayanhphu;
            cmd.Parameters.Add("@quayphim", SqlDbType.NText).Value = p.Quayphim;
            cmd.Parameters.Add("@nghenhac", SqlDbType.NText).Value = p.Nghenhac;
            cmd.Parameters.Add("@xemphim", SqlDbType.NText).Value = p.Xemphim;
            cmd.Parameters.Add("@radio", SqlDbType.NText).Value = p.Radio;
            cmd.Parameters.Add("@tivi", SqlDbType.NText).Value = p.Tivi;
            cmd.Parameters.Add("@ketnoitivi", SqlDbType.NText).Value = p.Ketnoitivi;
            cmd.Parameters.Add("@ghiam", SqlDbType.NText).Value = p.Ghiam;
            cmd.Parameters.Add("@luutrucuocgoi", SqlDbType.NText).Value = p.Luutrucuocgoi;
            cmd.Parameters.Add("@ungdungvanphong", SqlDbType.NText).Value = p.Ungdungvanphong;
            cmd.Parameters.Add("@ungdungkhac", SqlDbType.NText).Value = p.Ungdungkhac;
            cmd.Parameters.Add("@trochoi", SqlDbType.NText).Value = p.Trochoi;
            cmd.Parameters.Add("@hotrosim", SqlDbType.NText).Value = p.Hotrosim;
            cmd.Parameters.Add("@hotrojava", SqlDbType.NText).Value = p.Hotrojava;
            cmd.Parameters.Add("@kieudang", SqlDbType.NText).Value = p.Kieudang;
            cmd.Parameters.Add("@hedieuhanh", SqlDbType.NText).Value = p.Hedieuhanh;
            cmd.Parameters.Add("@phienbanhedieuhanh", SqlDbType.NText).Value = p.Phienbanhedieuhanh;
            cmd.Parameters.Add("@cpu", SqlDbType.NText).Value = p.Cpu;
            cmd.Parameters.Add("@ram", SqlDbType.NText).Value = p.Ram;
            cmd.Parameters.Add("@ngonngu", SqlDbType.NText).Value = p.Ngonngu;
            cmd.Parameters.Add("@qwerty", SqlDbType.NText).Value = p.Qwerty;
            cmd.Parameters.Add("@hotromang", SqlDbType.NText).Value = p.Hotromang;
            cmd.Parameters.Add("@loaipin", SqlDbType.NText).Value = p.Loaipin;
            cmd.Parameters.Add("@thoigiancho2g", SqlDbType.NText).Value = p.Thoigiancho2g;
            cmd.Parameters.Add("@thoigiancho3g", SqlDbType.NText).Value = p.Thoigiancho3g;
            cmd.Parameters.Add("@damthoai2g", SqlDbType.NText).Value = p.Damthoai2g;
            cmd.Parameters.Add("@damthoai3g", SqlDbType.NText).Value = p.Damthoai3g;
            cmd.Parameters.Add("@dungluongpin", SqlDbType.NText).Value = p.Dungluongpin;
            cmd.Parameters.Add("@image", SqlDbType.NText).Value = p.Image;
            cmd.Parameters.Add("@description", SqlDbType.NText).Value = p.Description;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public int Delete(int id)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Delete_Product";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@product_id",id);
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public DataTable GetAll()//Lay tat ca san pham
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_Product ORDER BY dateStart DESC";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }

    public DataTable Get_Most_View()//San pham dc xem nhieu nhat
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select top 5 * from Get_Most_View ORDER BY [view] DESC";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }

    public DataTable GetTopProductByCateId(int cateId)//Lay san pham theo danh muc sp
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_Product where category_id=" + cateId + " AND [top]=1";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }
    public DataTable GetProductByCateId(int cateId)//Lay san pham theo danh muc sp
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_Product where category_id=" + cateId + "";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }
    public DataTable GetProductProperty(int ProductId)//Lay san pham theo danh muc sp
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select Product.product_id, Product.category_id,Product.[image],Product.model,Product.price_output,Baohanh.title from Product inner join Baohanh on Product.baohanh_id=Baohanh.baohanh_id AND Product.product_id=" + ProductId + "";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }
    public DataTable GetSamePrice(int productId)//Lay san pham cung gia
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select top 5 * from Get_ProductAndImage where price_output<((select price_output from Product where product_id=" + productId + ")+500000) AND price_output >((select price_output from Product where product_id=" + productId + ")-500000)";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }

    public DataTable GetSameCategory(int productId)//Lay san pham cung hang
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select top 5 * from Select_All_Product where category_id=(select category_id from Product where product_id=" + productId + ")";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }

    public DataTable GetBaogia()//Lay san pham cung hang
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select Category.name,Product.model,Product.price_output,Baohanh.title from Product,Category,Baohanh where Category.category_id=Product.category_id AND Product.baohanh_id=Baohanh.baohanh_id";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }

    public DataTable GetProductDetail(int productId)//Lay san pham cung hang
    {
        string select = "select Product.model,Product.tag,Product.price_output,Category.name,Danhmucsanpham.name,Color.name,[Address].title,Phukienkemtheo.name,Baohanh.title";
        select += "from Product,Category,Danhmucsanpham,Color,[Address],Product_Address,Phukienkemtheo,Product_Phukienkemtheo,Baohanh,Product_Danhmuc,Product_Color,Product_Sale";
        select += "where Product.category_id=Category.category_id AND ";
        select += "(Product.product_id=Product_Danhmuc.product_id AND Danhmucsanpham.danhmuc_id=Product_Danhmuc.danhmuc_id) AND ";
        select += "(Product_Color.product_id=Product.product_id AND Color.color_id=Product_Color.color_id) AND ";
        select += "(Product.product_id=Product_Address.product_id AND [Address].address_id=Product_Address.address_id) AND ";
        select += "(Product.product_id=Product_Sale.product_id) AND ";
        select += "(Product.product_id=Product_Phukienkemtheo.product_id AND Phukienkemtheo.phukienkemtheo_id=Product_Phukienkemtheo.phukienkemtheo_id) AND ";
        select += "Product.baohanh_id=Baohanh.baohanh_id AND Product.product_id=" + productId + "";
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = select;
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }



    public DataTable GetByID(int id)
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_Product WHERE product_id=@product_id ORDER BY dateStart DESC";
            cmd.Parameters.AddWithValue("@product_id", id);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }
    public void UpdateView(int id)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "UPDATE Product set [view] = [view]+1 where product_id=@product_id";
            cmd.Parameters.AddWithValue("@product_id", id);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {

            throw;
        }
    }

    public bool checkProductId(int id)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_Product WHERE product_id=@product_id ORDER BY dateStart DESC";
            cmd.Parameters.AddWithValue("@product_id", id);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            SqlDataReader dr = cmd.ExecuteReader();
            return dr.HasRows;
        }
        catch (Exception)
        {

            return false;
            throw;
        }
    }

    public DataTable GetOther()
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT top 6 * FROM Select_All_Product ORDER BY dateStart DESC";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }
    public DataTable GetPhukienLienquan(int productId)
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            string select = "select Phukien.name,Phukien.phukien_id,Phukien.[image] from Product_Phukien";
            select += " inner join Phukien on Product_Phukien.phukien_id=Phukien.phukien_id";
            select += " inner join Product on Product_Phukien.product_id=Product.product_id AND Product.product_id=@productId";
            cmd.CommandText = select;
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@productId", productId);
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }
    public int insertProductColor(int product_id, int[] color)
    {
        int i = 0;
        foreach (int c in color)
        {
            try
            {
                if (c != 0)
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = "Insert_Product_Color";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@product_id", SqlDbType.Int).Value = product_id;
                    cmd.Parameters.Add("@color_id", SqlDbType.Int).Value = c;
                    i += cmd.ExecuteNonQuery();
                }

            }
            catch (Exception)
            {
                continue;
                throw;
            }
        }
        return i;
    }
    public int insertProductAddress(int product_id, int[] address)
    {
        int i = 0;
        foreach (int a in address)
        {
            try
            {
                if (a != 0)
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = "Insert_Product_Address";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@product_id", SqlDbType.Int).Value = product_id;
                    cmd.Parameters.Add("@address_id", SqlDbType.Int).Value = a;
                    i += cmd.ExecuteNonQuery();
                }

            }
            catch (Exception)
            {
                continue;
                throw;
            }
        }
        return i;
    }
    public int insertProductHDSD(int product_id, int[] hdsd)
    {
        int i = 0;
        foreach (int h in hdsd)
        {
            try
            {
                if (h != 0)
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = "Insert_Product_HDSD";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@product_id", SqlDbType.Int).Value = product_id;
                    cmd.Parameters.Add("@hdsd_id", SqlDbType.Int).Value = h;
                    i += cmd.ExecuteNonQuery();
                }

            }
            catch (Exception)
            {
                continue;
                throw;
            }
        }
        return i;
    }
    public int insertProductImage(int product_id, string[] images)
    {
        ProductImageController pi = new ProductImageController();

        int i = 0;
        foreach (string image in images)
        {
            ProductImage p = new ProductImage();
            p.Product_id = product_id;
            p.Image = image;
            p.DateStart = DateTime.Now;
            p.Order = 1;
            p.Status = true;
            i += pi.Insert(p);
        }
        return i;
    }
    public int insertProductNews(int product_id, int[] news)
    {
        int i = 0;
        foreach (int n in news)
        {
            try
            {
                if (n != 0)
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = "Insert_Product_News";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@product_id", SqlDbType.Int).Value = product_id;
                    cmd.Parameters.Add("@news_id", SqlDbType.Int).Value = n;
                    i += cmd.ExecuteNonQuery();
                }

            }
            catch (Exception)
            {
                continue;
                throw;
            }
        }
        return i;
    }
    public int insertProductPhukien(int product_id, int[] phukiens)
    {
        int i = 0;
        foreach (int pk in phukiens)
        {
            try
            {
                if (pk != 0)
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = "Insert_Product_Phukien";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@product_id", SqlDbType.Int).Value = product_id;
                    cmd.Parameters.Add("@phukien_id", SqlDbType.Int).Value = pk;
                    i += cmd.ExecuteNonQuery();
                }

            }
            catch (Exception)
            {
                continue;
                throw;
            }
        }
        return i;
    }
    public int insertProductRelate(int product_id, int[] relates)
    {
        int i = 0;
        foreach (int re in relates)
        {
            try
            {
                if (re != 0)
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = "Insert_Product_Relate";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@product_id", SqlDbType.Int).Value = product_id;
                    cmd.Parameters.Add("@product_relate", SqlDbType.Int).Value = re;
                    i += cmd.ExecuteNonQuery();
                }

            }
            catch (Exception)
            {
                continue;
                throw;
            }
        }
        return i;
    }
    public DataTable getProductRelate(int product_id)
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Product_Relate AS pr WHERE pr.product_id=@product_id ";
            cmd.Parameters.AddWithValue("@product_id", product_id);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }
    public int insertProductSale(int product_id, int[] sales)
    {
        int i = 0;
        foreach (int sa in sales)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandText = "Insert_Product_Sale";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@product_id", SqlDbType.Int).Value = product_id;
                cmd.Parameters.Add("@sale_id", SqlDbType.Int).Value = sa;
                i += cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                continue;
                throw;
            }
        }
        return i;
    }
<<<<<<< .mine

=======
    public int insertProductDanhmucsanpham(int product_id, int[] danhmucsanpham)
>>>>>>> .theirs    public DataTable User_Paging(int CurentPage, int PageSize)
    {
        DataTable dt = new DataTable();
        using (SqlCommand dbCmd = new SqlCommand("sp_Paging", con))
        {
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.Add(new SqlParameter("@CurentPage", CurentPage));
            dbCmd.Parameters.Add(new SqlParameter("@PageSize", PageSize));
            SqlDataAdapter da = new SqlDataAdapter(dbCmd);
            da.Fill(dt);
        }
        return dt;
    }
<<<<<<< .mine=======                    cmd.Parameters.Add("@danhmuc_id", SqlDbType.Int).Value = dmsp;
                    i += cmd.ExecuteNonQuery();
                }

            }
            catch (Exception)
            {
                continue;
                throw;
            }
        }
        return i;
    }

    public DataTable getProductDanhmucsanpham(int product_id)
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Product_Danhmuc AS pr WHERE pr.product_id=@product_id ";
            cmd.Parameters.AddWithValue("@product_id", product_id);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }
    public int countProductActive()
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_Product WHERE status=1";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt.Rows.Count;
        }
        catch (Exception)
        {

            return 0;
            throw;
        }
    }
>>>>>>> .theirs}